package Set;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMap {
    public static void main(String[] args) {
        testGet();
        testTravel();
        testList2Map();
    }

    private static void testTravel() {
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("D", 2);
        map.put("B", 2);
        map.put("C", 3);
        map.put("apple", 123);
        map.put("pear", 456);
        map.put("banana", 789);

        for (String key : map.keySet()) {
            System.out.println(key);
            System.out.println(map.get(key));
        }

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }

    private static void testList2Map() {
        List<Student2> list = List.of(
                new Student2("Bob", 78),
                new Student2("Alice", 85),
                new Student2("Brush", 66),
                new Student2("Newton", 99));
        var holder = new Students(list);
        System.out.println(holder.getScore("Bob") == 78 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Alice") == 85 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Tom") == -1 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Bob") == 78 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Bob") == 78 ? "测试成功!" : "测试失败!");
    }

    // key get Object
    private static void testGet() {
        Student s = new Student(new BigDecimal(1000000), "Xiao Ming", 99);
        Map<BigDecimal, Student> map = new HashMap<>();
        map.put(s.getBigDecimal(), s);

        Student target = map.get(new BigDecimal(1000000));
        System.out.println(target == s);
        System.out.println(target.getScore());
        Student another = map.get("Bob");
        System.out.println(another);
    }

    private static class Student {
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getScore() {
            return score;
        }

        public void setScore(int score) {
            this.score = score;
        }

        private String name;
        private int score;

        public BigDecimal getBigDecimal() {
            return bigDecimal;
        }

        public void setBigDecimal(BigDecimal bigDecimal) {
            this.bigDecimal = bigDecimal;
        }

        private BigDecimal bigDecimal;

        public Student(BigDecimal bigDecimal, String name, int score) {
            this.name = name;
            this.score = score;
            setBigDecimal(bigDecimal);
        }
    }

    private static class Students {
        List<Student2> list;
        Map<String, Integer> cache;

        Students(List<Student2> list) {
            this.list = list;
            cache = new HashMap<>();

//            for (Student2 student: list) {
//                cache.put(student.name, student.score);
//            }
        }

        /**
         * 根据name查找score，找到返回score，未找到返回-1
         */
        int getScore(String name) {
            // 先在Map中查找:
            Integer score = this.cache.get(name);
            if (score == null) {

                score = findInList(name);
                if (score != null) {
                    this.cache.put(name, score);
                }
            }
            return score == null ? -1 : score.intValue();
        }

        Integer findInList(String name) {
            for (var ss : this.list) {
                if (ss.name.equals(name)) {
                    return ss.score;
                }
            }
            return null;
        }
    }

    private static class Student2 {
        String name;
        int score;

        Student2(String name, int score) {
            this.name = name;
            this.score = score;
        }
    }
}
